package com.neuedu.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.neuedu.pojo.MailVo;
import com.neuedu.service.IRedisService;
import com.neuedu.util.CommonResult;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping("/mail")
public class MailController {

    @Resource
    JavaMailSender javaMailSender;
    @Resource
    RabbitTemplate rabbitTemplate;
    @Resource
    ObjectMapper objectMapper;
    @Resource
    IRedisService iRedisService;

    @PostMapping("/send")
    CommonResult send(MailVo mailVo){
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            if(mailVo.getSubject().equals("验证码")){
                System.out.println("放入ssdb");
                String code=mailVo.getContent().substring(10,6);
                String s=objectMapper.writeValueAsString(code);
                iRedisService.set(mailVo.getTo(),s,2*60);
            }
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom(mailVo.getFrom(),mailVo.getPersonal());
            helper.setTo(mailVo.getTo());
            helper.setSubject(mailVo.getSubject());
            helper.setText(mailVo.getContent(),true);
            javaMailSender.send(mimeMessage);



        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return  CommonResult.success("");
    }

    //测试，用来测试发送到mq消息
//    @PostMapping("/sendmq")
//    CommonResult sendmq(MailVo mailVo){
//        rabbitTemplate.convertAndSend("testqueue","143254");
//        return CommonResult.success("success");
//    }




    //从mq中读取消息，发送消息到个人邮箱
    @RabbitListener(queues = "mailqueue")
    public void sendmail(Message messsage){
        String body = new String(messsage.getBody());
        MailVo mailVo = null;
        try {
            mailVo = objectMapper.readValue(body,MailVo.class);
            if(mailVo.getSubject().equals("验证码")){
                System.out.println("放入ssdb");
                String code=mailVo.getContent().substring(9,15);
                String s=objectMapper.writeValueAsString(code);
                iRedisService.set(mailVo.getTo(),s,10*60);
            }
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom(mailVo.getFrom(),mailVo.getPersonal());
            helper.setTo(mailVo.getTo());
            helper.setSubject(mailVo.getSubject());
            helper.setText(mailVo.getContent(),true);
            javaMailSender.send(mimeMessage);
            System.out.println("输出mq到邮箱发送成功");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
